//
//  ExclusiveRegionTableViewCell.m
//  DayByDay
//
//  Created by dllo on 16/5/13.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ExclusiveRegionTableViewCell.h"
#import "ExclusiveCollectionViewCell.h"

@interface ExclusiveRegionTableViewCell ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong)UILabel *nameLabel;


@end

@implementation ExclusiveRegionTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createView];
    }
    return self;
}

- (void)setModel:(MusicModel *)model {
    _model = model;
    self.nameLabel.text = _model.name;
    [self.collectionView reloadData];
}

- (void)createView {
    self.nameLabel = [UILabel new];
    UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
    layout.itemSize = CGSizeMake((WIDTH - 30.0 / 736 * HEIGHT) / 3, (WIDTH - 30 / 736 * HEIGHT) / 3 + 50 / 736.0 * HEIGHT);
    layout.minimumInteritemSpacing = 5 / 736.0 * HEIGHT;
    layout.minimumLineSpacing = 10 / 736.0 * HEIGHT;
    layout.sectionInset = UIEdgeInsetsMake(10 / 736.0 * HEIGHT, 10 / 736.0 * HEIGHT, 10 / 736.0 * HEIGHT, 10 / 736.0 * HEIGHT);
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 40 / 736.0 * HEIGHT, WIDTH, self.contentView.frame.size.height - 70 / 736.0 * HEIGHT) collectionViewLayout:layout];
    [self.contentView addSubview:self.nameLabel];
    [self.contentView addSubview:self.collectionView];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerClass:[ExclusiveCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([ExclusiveCollectionViewCell class])];
    self.collectionView.backgroundColor = [UIColor clearColor];
    self.backgroundColor = [UIColor clearColor];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.model.data.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    ExclusiveCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([ExclusiveCollectionViewCell class]) forIndexPath:indexPath];
    cell.model = self.model.data[indexPath.row];
    cell.collectionViewTag = collectionView.tag;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    self.exclusiveBlock(collectionView.tag, self.model.data[indexPath.row]);
//    NSLog(@"%@",indexPath);
//    NSLog(@"%ld",collectionView.tag);
    
}

- (void)layoutSubviews {
    self.nameLabel.frame = CGRectMake(WIDTH /  4, 10 / 736.0 * HEIGHT, WIDTH / 2, 30 / 736.0 * HEIGHT);
    self.collectionView.frame = CGRectMake(0, 40 / 736.0 * HEIGHT, WIDTH, self.contentView.frame.size.height - 40 / 736.0 * HEIGHT);
    self.nameLabel.textAlignment = 1;
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
